const Service = require('egg').Service;
const { Sequelize, Op } = require('sequelize');

class TopicService extends Service {
    // 添加用service
    async add(topic) {
        const { ctx } = this;
        const result = await ctx.model.Topic.create(topic);
        return result;
    }
    // 分页查询用service
    async findList(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Topic.findAndCountAll({
            where: {
                title: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
                {
                    model:ctx.model.Student,
                    as:'student'
                }
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    // 根据id查找对应的值
    async findById(id) {
        const { ctx } = this;
        const result = await ctx.model.Topic.findOne({
            where:{
                id:id
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
                {
                    model:ctx.model.Student,
                    as:'student'
                }
            ],
        });
        return result;
    }
    // 修改
    async modify(topic) {
        const { ctx } = this;
        const { 
            id, 
            title,
            stu_id,
            description,
            pass,
        } = topic;
        const result = await ctx.model.Topic.update({
            title,
            stu_id,
            description,
            pass,
        }, {
            where: { id }
        });
        return result;
    }
    // 删除
    async remove(id) {
        const { ctx } = this;
        const result = await ctx.model.Topic.update({
            deleted:1
        },{
            where: { id }
        });
        return result;
    }
    // 分页查询用service
    async findNotPass(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Topic.findAndCountAll({
            where: {
                title: {
                    [Op.like]: '%' + keywords + '%'
                },
                pass:0,
                deleted:0
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    // 分页查询用service
    async findTopicForTeacher(keywords, pageIndex, pageSize, teacher_id) {
        const { ctx } = this;
        const result = await ctx.model.Topic.findAndCountAll({
            where: {
                title: {
                    [Op.like]: '%' + keywords + '%'
                },
                // stu_id:null,
                deleted:0,
                // 获得老师的id
                ins_id:teacher_id
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
                {
                    model:ctx.model.Student,
                    as:'student'
                }
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    async findTopicStudentList(keywords, pageIndex, pageSize, teacher_id) {
        const { ctx } = this;
        const result = await ctx.model.Topic.findAndCountAll({
            where: {
                title: {
                    [Op.like]: '%' + keywords + '%'
                },
                stu_id:{
                    [Op.ne]: null,
                },
                deleted:0,
                // 获得老师的id
                ins_id:teacher_id
            },
            include:[
                {
                    model:ctx.model.Student,
                    as:'student'
                }
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    async findByStudentId(stu_id) {
        const { ctx } = this;
        const result = await ctx.model.Topic.findOne({
            where:{
                stu_id
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
                {
                    model:ctx.model.Student,
                    as:'student'
                }
            ],
        });
        return result;
    }
    // 选取出需要评阅的选题、学生、老师的信息
    async findByneedReview(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Topic.findAndCountAll({
            where: {
                title: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
                {
                    model:ctx.model.Student,
                    as:'student',
                    where:{
                        progress:13
                    }
                }
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
}
module.exports = TopicService;
